package com.common.service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.common.dao.GenericDao;

/**
 * This class serves as the Base class for all other Managers - namely to hold common CRUD methods that they might all use. You should only need to extend this class when your require custom CRUD
 * logic.
 * 
 */
@Transactional
public class GenericManagerImpl<T, PK extends Serializable> implements
		GenericManager<T, PK> {

	/**
	 * GenericDao instance, set by constructor of this class.
	 */
	protected GenericDao<T, PK> genericDao;

	/**
	 * Public constructor for creating a new GenericManagerImpl.
	 * 
	 * @param genericDao
	 *            the GenericDao to use for persistence
	 */
	public GenericManagerImpl(final GenericDao<T, PK> genericDao) {
		this.genericDao = genericDao;
	}

	@Transactional(readOnly = true)
	public List<T> getAll() throws ServiceException {
		// return genericDao.getAllDistinct();
		return genericDao.getAll();
	}

	@Transactional(readOnly = true)
	public List<T> getAll(int start, int limit) throws ServiceException {
		return genericDao.findByPagination(null, start, limit);
	}

	@Transactional(readOnly = true)
	public T get(PK id) throws ServiceException {
		return genericDao.get(id);
	}

	@Transactional(readOnly = true)
	public boolean exists(PK id) throws ServiceException {
		return genericDao.exists(id);
	}

	public T save(T object) throws ServiceException {
		return genericDao.save(object);
	}

	public void saveAll(Collection<T> col) throws ServiceException {
		genericDao.saveAll(col);
	}

	public void remove(PK id) throws ServiceException {
		genericDao.remove(id);
	}

	public void removeAll(Collection<T> col) throws ServiceException {
		genericDao.removeAll(col);
	}
}
